//
//  MovieDetailViewController.m
//  UI-21-豆豆豆瓣
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 Wang Hao. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "AppTools.h"

#import "DetailView.h"
#import "DatabaseSingleton.h"


@interface MovieDetailViewController ()<DetailViewDelegate>

@property (nonatomic, retain) DetailView *myDetail;



@property (nonatomic, retain) UIScrollView *scroll;
@end

@implementation MovieDetailViewController


- (void)dealloc
{
    [_movie release];
    [_myDetail release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.movie.movieName;
    self.view.backgroundColor = [UIColor whiteColor];
    //渲染取消
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *rightImage = [UIImage imageNamed:@"btn_nav_share.png"];
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)] autorelease];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)] autorelease];
    // Do any additional setup after loading the view.
     [self getWorking];
    //CGFloat height = [DetailView heightForPlot:self.movie.plot_simple];
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
    [self.view addSubview:self.scroll];
    self.scroll.contentSize = CGSizeMake(375,  325);
    self.myDetail = [[DetailView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
    self.myDetail.collectDelegate = self;
    [self.scroll addSubview:self.myDetail];
    
   
    
    [_myDetail release];
    [_scroll release];
    
}


-(void)getWorking{
     NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@", self.movie.movieId];
    [AppTools getUrl:urlStr block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSMutableDictionary *resultDic = [dic objectForKey:@"result"];
        NSString *reason = [dic objectForKey:@"reason"];
        if ([reason isEqualToString:@"成功的返回"]) {
            
            Movie *movie = [[Movie alloc] init];
            [movie setValuesForKeysWithDictionary:resultDic];
            self.myDetail.movDetail = movie;
            
            [movie release];
            
        }else{
            
            NSLog(@"请求网络失败");
        }
        
        
        self.scroll.contentSize = CGSizeMake(375, self.myDetail.height + 475);

        
    }];
    
    
}

#warning 收藏
-(void)collect{
    
    
    NSString *nameU = [[NSUserDefaults standardUserDefaults] objectForKey:@"用户名"];
    //NSLog(@"+++++++++++++++++++++nameU++++++%@",nameU);
    
    NSString *a = [[NSUserDefaults standardUserDefaults] objectForKey:@"1"];
    if ([a isEqualToString:@"1"]) {
        
        [[DatabaseSingleton shareDatabaseSingleton] openDB];
        NSMutableArray *array = [[DatabaseSingleton shareDatabaseSingleton] selectcollect:nameU];
        NSMutableArray *movieNameArray = [NSMutableArray array];
        for (Movie *mov in array) {
            
            [movieNameArray addObject:mov.release_date];
        }
        BOOL result = [movieNameArray containsObject:self.myDetail.movDetail.release_date];
        //NSLog(@"==============self.movie.release_date%@",self.movDetail.release_date);
        if (result) {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经收藏过" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            
            
            
        }else{
            
            [self.myDetail.collectButton setBackgroundImage:[UIImage imageNamed:@"ic_star_red@2x"] forState:UIControlStateNormal];
            [[DatabaseSingleton shareDatabaseSingleton] insertData:self.movie name:nameU];
            NSLog(@"====%@", self.movie.movieName);
            [[DatabaseSingleton shareDatabaseSingleton] insertcollect:self.myDetail.movDetail name:nameU];
            [[NSUserDefaults standardUserDefaults]setObject:self.movie.movieName forKey:@"电影名"];
            //NSLog(@"==================%@",self.myDetail.movDetail);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }else{
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }

       
}

-(void)leftAction:(UIBarButtonItem *)left{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}
-(void)rightAction:(UIBarButtonItem *)right{
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
